const User = require('../models/user.model.js')
const util = require('../util/util.js')
const responseResultData = util.responseResultData
// 创建新的用户对象
exports.create = (req, res) => {
  // 验证请求
  if (!req.body) {
    responseResultData(res, 200, false,  '参数不能为空', '')
    return
  }
  if (!req.body.userName) {
    responseResultData(res, 200, false,  '用户名不能为空', '')
    return
  }
  if (!req.body.passWord) {
    responseResultData(res, 200, false,  '密码不能为空', '')
    return
  }
  if (req.body.passWord !== req.body.confirmPassWord) {
    responseResultData(res, 200, false,  '输入密码不一致', '')
    return
  }
  // 查询用户是否存在
  User.getByName(req.body.userName, (err, data) => {
    if (err) {
      responseResultData(res, 200, false,  err.message || '查找用户错误', '')
      return
    }
    if(data) {
      responseResultData(res, 200, false, '用户名重复')
      return
    }
    // 创建新的对象
    const salt = util.generateRandomNumber()
    const passWord = util.encryption(req.body.passWord)
    const userModel = User.createNew({
      userName: req.body.userName,
      passWord: passWord,
      trueName: req.body.trueName ?? '',
      telephone: req.body.telephone ?? '',
      salt: salt,
      email: req.body.email ?? ''
    })
    User.create([userModel], (err, data) => {
      if (err) {
        responseResultData(res, 200, false,  err.message || '创建用户错误', '')
        return
      }
      responseResultData(res, 200, true, '', '')
    })
  })
  
}
// 根据ID查询用户
exports.getDetail = (req, res) => {
  // 验证请求
  if (!req.query || !req.query.id) {
    responseResultData(res, 200, false,  '参数不能为空', '')
    return
  }

  // 查找
  User.getById(req.query.id, (err, data) => {
    if (err) {
      responseResultData(res, 200, false,  err.message || '查找用户错误', '')
      return
    }
    if(!data) {
      responseResultData(res, 200, false, '未找到用户信息')
      return
    }
    responseResultData(res, 200, true, '', data)
  })
}
// 获取用户列表
exports.getList = (req, res) => {
  console.log('####22##', req.auth)
  // 查找所有
  User.getList((err, data) => {
    if (err) {
      responseResultData(res, 200, false,  err.message || '获取用户列表错误', '')
      return
    }
    responseResultData(res, 200, true, '', data)
  })
}

// 根据ID更新用户
exports.update = (req, res) => {
  // 验证请求
  if (!req.body) {
    responseResultData(res, 200, false,  '参数不能为空', '')
    return
  }
  if (req.body.passWord && req.body.passWord !== req.body.confirmPassWord) {
    responseResultData(res, 200, false,  '输入密码不一致', '')
    return
  }
  const body = {
    id: req.body.id,
    trueName: req.body.trueName ?? '',
    telephone: req.body.telephone ?? '',
    email: req.body.email ?? ''
  }
  if (req.body.passWord) {
    body.passWord = util.encryption(req.body.passWord)
  }
  // 更新
  User.updateById(body, (err, data) => {
    if (err) {
      responseResultData(res, 200, false,  err.message || '更新用户错误', '')
      return
    }
    responseResultData(res, 200, true, '', data)
  })

}
// 删除用户
exports.delete = (req, res) => {
  // 验证请求
  if (!req.body || !req.body.id) {
    responseResultData(res, 200, false,  '参数不能为空', '')
    return
  }
  // 删除
  User.delete(req.body.id, (err) => {
    if (err) {
      responseResultData(res, 200, false,  err.message || '删除用户错误', '')
      return
    }
    responseResultData(res, 200, true, '', '')
  })
}
// 设置角色
exports.setRole = (req, res) => {
  // 验证请求
  if (!req.body || !req.body.roleIds || !req.body.roleIds.length === 0) {
    responseResultData(res, 200, false,  '参数不能为空', '')
    return
  }
  console.log('######', req.auth)
  let data = []
  for (const roleId of req.body.roleIds) {
    data.push({
      userId: req.auth.id,
      appRoleId: roleId
    })
  }
  // 设置角色
  User.setRole(req.auth.id, data, (err) => {
    if (err) {
      responseResultData(res, 200, false,  err.message || '设置角色错误', '')
      return
    }
    responseResultData(res, 200, true, '', '')
  })
}

